package com.bw.jetpacktest.room;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import com.bw.jetpacktest.R;
import com.bw.jetpacktest.Test;

import java.util.List;

public class RoomActivity extends AppCompatActivity implements View.OnClickListener {
    private  AppDatabase db;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        findViewById(R.id.insert_btn).setOnClickListener(this);
        findViewById(R.id.sel_btn).setOnClickListener(this);
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "database-name").build();
    }

    @Override
    public void onClick(View v) {
        if (R.id.insert_btn == v.getId()){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    UserEntity entity = new UserEntity();
                    entity.name = "hehe";
                    db.userDao().insertUser(entity);
                }
            }.start();
        }else {
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    List<UserEntity>list=db.userDao().queryAll();
                    Log.e("ZXY",list.get(0).name);
                }
            }.start();
        }
    }
}
